<template>
  <div>
    <m-infinite-list
      v-model="isLoading"
      :isFinished="isFinished"
      @onLoad="getPexelsData"
    >
      <m-waterfall
        :data="pexelsList"
        :column="isMobileTerminal ? 2 : 5"
        :picturePreReading="true"
        class="w-full px-1"
        nodeKey="id"
      >
        <template v-slot="{ item, width }">
          <item-vue :data="item" :width="width" @click="onToPins" />
        </template>
      </m-waterfall>
    </m-infinite-list>
    <!-- 图片详情页 -->
    <transition
      :class="false"
      @before-enter="beforeEnter"
      @enter="enter"
      @leave="leave"
    >
      <pins-vue v-if="isVisiblePins" :id="currentPins.id"></pins-vue>
    </transition>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'
import { getPexelsList } from '@/api/pexels'
import ItemVue from './item.vue'
import { isMobileTerminal } from '@/utils/flexible'
import { useStore } from 'vuex'
import PinsVue from '@/views/pins/components/pins.vue'
import gsap from 'gsap'
import { useEventListener } from '@vueuse/core'
const isVisiblePins = ref(false)
const currentPins = ref({})
// 点击图片
const onToPins = (item) => {
  history.pushState(null, null, `/pins/${item.id}`)
  currentPins.value = item
  isVisiblePins.value = true
}
const beforeEnter = (el) => {
  gsap.set(el, {
    scaleX: 0,
    scaleY: 0,
    transformOrigin: '0 0',
    translateX: currentPins.value.location?.translateX,
    translateY: currentPins.value.location?.translateY,
    opacity: 0
  })
}
const enter = (el, done) => {
  gsap.to(el, {
    duration: 0.3,
    scaleX: 1,
    scaleY: 1,
    translateX: 0,
    translateY: 0,
    opacity: 1,
    onComplete: done
  })
}
const leave = (el) => {
  gsap.to(el, {
    scaleX: 0,
    scaleY: 0,
    duration: 0.3,
    translateX: currentPins.value.location?.translateX,
    translateY: currentPins.value.location?.translateY,
    opacity: 0
  })
}
useEventListener(window, 'popstate', () => {
  isVisiblePins.value = false
})
const pexelsList = ref([])
let query = {
  page: 1,
  size: 20
}

const store = useStore()
const isLoading = ref(false)
const isFinished = ref(false)
const getPexelsData = async () => {
  isLoading.value = true
  if (isFinished.value) return
  if (pexelsList.value.length) {
    query.page += 1
  }
  const res = await getPexelsList(query)
  if (query.page === 1) {
    pexelsList.value = res.list
  } else {
    pexelsList.value.push(...res.list)
  }
  // 判断是否加载完成
  if (pexelsList.value.length === res.total) {
    isFinished.value = true
  }
  isLoading.value = false
}
const resetQuery = (newQuery) => {
  query = query = { ...query, ...newQuery }
  isFinished.value = false
  pexelsList.value = []
}
watch(
  () => store.getters.currentCategory,
  (currentCategory) => {
    resetQuery({
      page: 1,
      categoryId: currentCategory.id
    })
  }
)
watch(
  () => store.getters.searchText,
  (val) => {
    console.log('关键字搜索...')
    // 重置请求参数
    resetQuery({
      page: 1,
      searchText: val
    })
  }
)
</script>

<style lang="scss" scoped></style>
